package com.lzx.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

import com.lzx.actionform.NewsForm;
import com.lzx.actionform.UploadFile;
import com.lzx.utils.FileUtil;

public class NewsAction extends DispatchAction {

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		NewsForm newsForm = (NewsForm) form;
		// News news = new News();
		//
		// news.setTitle(newsForm.getTitle());
		// news.setSubTitle(newsForm.getSubTitle());
		// news.setContent(newsForm.getContent());
		// news.setCreateDate(new Date());
		//
		// String modelId = request.getParameter("modelId");
		// Model model = (Model) companyService.getObject(Model.class, modelId);
		// news.setModel(model);

		// 保存图片
		String bathPath = request.getSession().getServletContext().getRealPath(
				"/")
				+ "news";
		// System.out.println(bathPath);
		StringBuffer sb = new StringBuffer();
		List myFiles = newsForm.getMyFiles();

		for (int i = 0; i < myFiles.size(); i++) {
			UploadFile uploadFile = (UploadFile) myFiles.get(i);
			
			if (uploadFile != null && "".equals(uploadFile.getFile())) {
				FormFile file = uploadFile.getFile();
				String fileType = file.getFileName().substring(
						file.getFileName().lastIndexOf(".") + 1,
						file.getFileName().length());
				String fileName = FileUtil.getRandomFileName() + "." + fileType;

				if (file.getFileSize() > 0) {
					FileUtil
							.saveFile(file.getInputStream(), bathPath, fileName);
				}
				sb.append("/news/" + fileName + ";");
			}
		}

		// news.setPicturePath(sb.toString());
		// companyService.addObject(news);
		return mapping.findForward("add");
	}

}